package com.lite.business.service.accessory.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lite.business.convert.accessory.AccessoryConvert;
import com.lite.business.entity.accessory.Accessory;
import com.lite.business.exception.BusinessException;
import com.lite.business.mapper.accessory.AccessoryMapper;
import com.lite.business.service.accessory.AccessoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author stranger
 * @since 2022-10-25
 */
@Service
public class AccessoryServiceImpl extends ServiceImpl<AccessoryMapper, Accessory> implements AccessoryService {


    @Autowired
    AccessoryMapper accessoryMapper;

    @Autowired
    AccessoryConvert accessoryConvert;

    @Override
    @Cacheable(value = "getAccessoryInfoById", key = "#cmoId")
    public Accessory getAccessoryInfoById(Integer cmoId, Integer accessoryId) throws BusinessException {
        Accessory accessory = accessoryMapper.selectOne(new LambdaQueryWrapper<Accessory>()
                .eq(Accessory::getCmodId, cmoId)
                .eq(Accessory::getId, accessoryId));

        if (Objects.isNull(accessory)) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询附属品信息失败");
        }

        return accessory;
    }

    @Override
    @Cacheable("listAccessoryByPage")
    public List<Accessory> listAccessoryByPage(Integer cmodId, Integer page, Integer size) throws BusinessException {
        List<Accessory> accessoryList = this.page(new Page<>(page, size),
                new LambdaQueryWrapper<Accessory>().eq(Accessory::getCmodId, cmodId)).getRecords();
        if (Objects.isNull(accessoryList)) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询附属品信息失败");
        }
        return accessoryList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getAccessoryInfoById", key = "#accessoryId"),
                    @CacheEvict(value = "listAccessoryByPage", allEntries = true)
            }
    )
    public Boolean updateAccessoryInfoById(Integer cmodId, Integer accessoryId, Accessory newAccessory) throws BusinessException {
        int count = accessoryMapper.update(newAccessory,
                new LambdaUpdateWrapper<Accessory>().eq(Accessory::getCmodId, cmodId).eq(Accessory::getId, accessoryId));
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "更新附属品信息失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getAccessoryInfoById", key = "#accessoryId"),
                    @CacheEvict(value = "listAccessoryByPage", allEntries = true)
            }
    )
    public Boolean removeAccessoryInfoById(Integer cmodId, Integer accessoryId) throws BusinessException {
        int count = accessoryMapper.delete(
                new LambdaQueryWrapper<Accessory>()
                        .eq(Accessory::getCmodId, cmodId)
                        .eq(Accessory::getId, accessoryId));
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除附属品信息失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getAccessoryInfoById", key = "#accessory.id"),
                    @CacheEvict(value = "listAccessoryByPage", allEntries = true)
            }
    )
    public Boolean addAccessoryInfo(Accessory accessory) throws BusinessException {
        int count = accessoryMapper.insert(accessory);
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "新增附属品信息失败");
        }
        return true;
    }
}
